/*!
    \copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.
                All Rights Reserved.
                Qualcomm Technologies International, Ltd. Confidential and Proprietary.
    \file
    \ingroup    connection_manager
    \brief      The events sent from the connection_manager using the Event Bus
*/

#include "le_audio_core_channel_event_spec.h"
#include "le_core_channel_event_spec.h"
#include "event_bus_publish.h"
#include "connection_manager_le_event_send.h"
#include <connection_manager_list.h>
#include "connection_manager_msg.h"
#include "connection_manager_config.h"

#include <logging.h>

#ifdef USE_SYNERGY

/*! \brief Send message through Event Bus

 \param event event bus event to publish.
 \param data data associated with the event
 \param data_size size of the data

 */
void static conManager_PublishEvent(le_audio_core_event_t event, void * data, uint16 data_size)
{
    EventBus_Publish(EVENT_BUS_CHANNEL_LE_AUDIO_CORE, event, data, data_size);
}

void conManager_SendDisconnectionCompleteEvent(uint16_t connection_handle, hci_status reason, acl_link_type_t link_type)
{
    LE_AUDIO_CORE_DISCONNECTION_COMPLETE_T event = {0};

    event.status = hci_success;
    event.connection_handle= connection_handle;
    event.reason = reason;
    event.type = link_type;
    DEBUG_LOG("conManager_SendDisconnectionCompleteEvent");
    conManager_PublishEvent(le_audio_core_disconnection_complete, &event, sizeof(LE_AUDIO_CORE_DISCONNECTION_COMPLETE_T));
}

void conManager_SendEnhancedConnectionCompleteEvent(const CsrBtCmLeEventConnectionInd *ind, uint16_t conn_handle)
{
    LE_AUDIO_CORE_ENHANCED_CONNECTION_COMPLETE_T event = {0};

    event.subevent_code = 0x01;
    event.status = ind->connectStatus;
    event.conn_handle = conn_handle;
    event.role = ind->role;
    event.conn_interval = ind->interval;
    event.Peer_Address_Type = TYPED_BDADDR_PUBLIC;
    memcpy(&event.peer_address, &ind->deviceAddr.addr, sizeof(bdaddr));
    /* Need To Correct once synergy send correct address*/
    memcpy(&event.local_res_pvt_addr, &ind->deviceAddr.addr, sizeof(bdaddr));
    /* Need To Correct once synergy send correct address*/
    memcpy(&event.peer_res_pvt_addr, &ind->deviceAddr.addr, sizeof(bdaddr));
    event.conn_latency = ind->latency;
    event.supervision_Timeout = ind->timeout;
    event.max_clok_accuracy = ind->accuracy;

    DEBUG_LOG("conManager_SendEnhancedConnectionCompleteEvent");
    conManager_PublishEvent(le_audio_core_enhanced_connection_complete, &event, sizeof(LE_AUDIO_CORE_ENHANCED_CONNECTION_COMPLETE_T));
}

void conManager_SendConnectionUpdateCompleteEvent(hci_status status, uint16_t connection_handle, uint16_t  conn_interval,
                                                                               uint16_t conn_latency, uint16_t supervision_Timeout )
{
    LE_AUDIO_CORE_CONNECTION_UPDATE_COMPLETE_T event = {0};

    event.subevent_code = 0x03;
    event.status = status;
    event.conn_handle = connection_handle;
    event.conn_interval = conn_interval;
    event.conn_latency = conn_latency;
    event.supervision_Timeout = supervision_Timeout;

    DEBUG_LOG("conManager_SendConnectionUpdateCompleteEvent");
    conManager_PublishEvent(le_audio_core_connection_update_complete, &event, sizeof(LE_AUDIO_CORE_CONNECTION_UPDATE_COMPLETE_T));
}

void conManager_SendPathLossThresholdReportEvent(uint16_t conn_handle, uint8_t current_path_loss, uint8_t zone_entered)
{
    le_core_path_loss_threshold_report_t threshold_report = 
    {
        .subevent_code = 0x20,
        .conn_handle = conn_handle,
        .current_path_loss = current_path_loss,
        .zone_entered = zone_entered,
    };
    EventBus_Publish(EVENT_BUS_CHANNEL_LE_CORE, le_core_path_loss_threshold_report, &threshold_report, sizeof(threshold_report));
}

void conManager_SendSubrateChangeEvent(uint16_t conn_handle, 
                                       hci_status status,
                                       uint16 subrate_factor,
                                       uint16 peripheral_latency,
                                       uint16 continuation_number,
                                       uint16 supervision_timeout)
{
    le_core_subrate_change_t subrate_change = {0};

    subrate_change.subevent_code = 0x23;
    subrate_change.status = status;
    subrate_change.conn_handle = conn_handle;
    subrate_change.subrate_factor = subrate_factor;
    subrate_change.peripheral_latency = peripheral_latency;
    subrate_change.continuation_number = continuation_number;
    subrate_change.supervision_timeout = supervision_timeout;

    EventBus_Publish(EVENT_BUS_CHANNEL_LE_CORE, le_core_subrate_change, &subrate_change, sizeof(subrate_change));
}

#endif /* USE_SYNERGY */

